﻿using AntdUI;

namespace TestPressure
{
    public partial class TemplateSelectForm : BaseForm
    {

        #region 自定义方法

        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<Template> list = SqlSugarHelper.Inquireable<Template>(null);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<Template> list)
        {
            var antItemList = new List<TemplateItem>();
            foreach (Template item in list)
            {
                string first = string.Empty;
                if (item.first_stage_pressure > 0 && item.first_stage_minute > 0)
                {
                    first = string.Format("压力:{0}--时长：{1}", item.first_stage_pressure.ToString(), item.first_stage_minute.ToString());
                }

                string second = string.Empty;
                if (item.second_stage_pressure > 0 && item.second_stage_minute > 0)
                {
                    second = string.Format("压力:{0}--时长：{1}", item.second_stage_pressure.ToString(), item.second_stage_minute.ToString());
                }

                string third = string.Empty;
                if (item.third_stage_pressure > 0 && item.third_stage_minute > 0)
                {
                    third = string.Format("压力:{0}--时长：{1}", item.third_stage_pressure.ToString(), item.third_stage_minute.ToString());
                }

                string rigor = string.Empty;
                if (item.rigor_stage_pressure > 0 && item.rigor_stage_minute > 0)
                {
                    rigor = string.Format("压力:{0}--时长：{1}", item.rigor_stage_pressure.ToString(), item.rigor_stage_minute.ToString());
                }

                string intensity = string.Empty;
                if (item.intensity_stage_pressure > 0 && item.intensity_stage_minute > 0)
                {
                    intensity = string.Format("压力:{0}--时长：{1}", item.intensity_stage_pressure.ToString(), item.intensity_stage_minute.ToString());
                }

                string pipe_position = string.Empty;
                if (item.pipe_position is not null)
                {
                    pipe_position = item.pipe_position;
                }

                string edit = "否";
                if (item.edit_enable == 1)
                {
                    edit = "是";
                }
                antItemList.Add(new TemplateItem(item.template_number, pipe_position, item.design_pressure,
                    first, second, third, rigor, intensity, edit));
            }
            Table.DataSource = antItemList;
        }
        #endregion

        #region 系统方法
        public TemplateSelectForm()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterParent;
            FullScreen = true;
        }

        private void TemplateSelectForm_Load(object sender, EventArgs e)
        {
            Header.Text = "模板选择";
            Table.Height = Height - Header.Height;
            Table.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("template_number","模板编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("pipe_position","管道位置",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("design_pressure","设计压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("first_stage","第一阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("second_stage","第二阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("third_stage","第三阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("rigor_stage","严密阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("intensity_stage","强度阶段",AntdUI.ColumnAlign.Center),
            };
            Spin.Config config = new Spin.Config
            {
                Text = "正在加载中...",
            };
            Table.Spin(config, () =>
            {
                LoadList();
            });
        }
        private void Table_CellDoubleClick(object sender, TableClickEventArgs e)
        {
            if (this.Owner is not null)
            {
                if (e.Record is not null)
                {
                    TemplateItem item = (TemplateItem)e.Record;
                    ProjectAddForm frm = (ProjectAddForm)this.Owner;
                    frm.templateObj = item;
                    frm.LoadTemplateInfo();
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
        }
        #endregion
    }
}
